<?php
/**
 * Created by PhpStorm.
 * User: 29620
 * Date: 2021/10/21
 * Time: 9:01
 */
namespace app\admin\model;

use app\admin\controller\Good;
use app\common\traits\Jump;
use think\Model;
use think\model\concern\SoftDelete;

class Goods extends BaseModel
{
    use SoftDelete;
    //使用搜索器
    public function searchKeywordsAttr($q,$v,$d)
    {
        $q->where('g.name','like','%'.$v.'%');
    }

    public function searchIsShowAttr($q,$v,$d)
    {
        $q->where('is_show','=',$v);
    }

    public function searchIsHotAttr($q,$v,$d)
    {
        $q->where('is_hot','=',$v);
    }

    public function searchIsDownAttr($q,$v,$d)
    {
        $q->where('is_down','in',$v);
    }

    public function searchAddTimeAttr($q,$v,$d)
    {
        $q->where('g.addtime','> TIME',$v);
    }

    public function searchAddTimesAttr($q,$v,$d)
    {
        $q->where('g.addtime','< TIME',$v);
    }

    public function searchMinAttr($q,$v,$d)
    {
        $q->where('g.price','>',$v);
    }

    public function searchMaxAttr($q,$v,$d)
    {
        $q->where('g.price','',$v);
    }

    public function searchCidAttr($q,$v,$d)
    {
        $q->where('g.cid','=',$v);
    }

    public static function getGoods($where,$input)
    {
        $list = Goods::alias('g')
            ->join('ecm_category c','g.cid = c.id')
            ->field('g.*')
            ->field('c.name as cname')
            ->withSearch(array_keys($where),$where)
            ->paginate(['list_rows'=>5,'query'=>$input]);

        if($list->count() == 0){
            $list = Goods::alias('g')
                ->join('ecm_category c','g.cid = c.id')
                ->field('g.*')
                ->field('c.name as cname')
                ->withSearch(array_keys($where),$where)
                ->paginate([
                    'list_rows'=>5,
                    'query'=>$input,
                    'page'=>$list->lastPage()
                ]);
        }
        return $list;
    }

    //public static function addGood($data)
    //{
    //    try{
    //        $res = Goods::create($data);
    //        $msg = '添加成功';
    //    }catch (\Exception $e){
    //        $res = false;
    //        $msg = $e->getMessage();
    //    }
    //    return [$res,$msg];
    //}
    //
    //public static function GoodEdit($id)
    //{
    //    $edit = Goods::where('id','=',$id)->find();
    //    return $edit;
    //}
    //
    //public static function EditGood($data,$id)
    //{
    //    try{
    //        $res = Goods::where('id','=',$id)->update($data);
    //        $msg = '修改成功';
    //    }catch (\Exception $e){
    //        $res = false;
    //        $msg = $e->getMessage();
    //    }
    //    return [$res,$msg];
    //}
    
}































